using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class SunNumber : MonoBehaviour
{
    public static SunNumber Instance { get; private set; }
    private void Awake()
    {
        Instance = this;
    }
    [SerializeField]
    private int sunPoint;
    private Vector3 SunPost;
    private float produceTime=5f;
    private float produceTimer=0;
    public GameObject sunPrefeb;
    private bool isProduce = false;
    private void Start()
    {
        UpdateSunPointText();
        CalcSunPos();
        GmaeStart();
    }
    private void Update()
    {
        if (isProduce)
        {
            ProduceSun();
        }
    }
    public void GmaeStart()
    {
        isProduce = true;
    }

    public int SunPoint
    {
        get { return sunPoint; }
    }
    public TextMeshProUGUI sunpointtext;
    private void UpdateSunPointText()
    {
        sunpointtext.text = SunPoint.ToString();
    }
    public void SubSun(int sun)
    {
        sunPoint -= sun;
        UpdateSunPointText();
    }
    public void AddSun(int sun)
    {
        sunPoint += sun;
        UpdateSunPointText();
    }
    public Vector3 GetSunPos()
    {
        return SunPost;
    }
    public void CalcSunPos()
    {
        Vector3 position = Camera.main.ScreenToWorldPoint(sunpointtext.transform.position);
        position.z = 0;
        SunPost = position;
    }
    void ProduceSun()
    {
        produceTimer += Time.deltaTime;
        if (produceTimer > produceTime)
        {
            produceTimer = 0;
            Vector3 position = new Vector3(Random.Range(-7f, 4f), 6f, -1f);
            GameObject go = GameObject.Instantiate(sunPrefeb,position,Quaternion.identity);

            position.y = Random.Range(-4f, 3f);
            go.GetComponent<Sun>().MoveTo(position);
        }
    }
}
